package com.example.keli.ui.reward;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.keli.R;
import com.example.keli.base.utils.DataUtil;
import com.example.keli.base.utils.GlideUtil;
import com.example.keli.base.utils.LogUtils;
import com.example.keli.ui.center.ActiveDetailsActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * description $desc$
 * created by jerry on 2019/3/18.
 */
public class RewardListAdapter extends BaseQuickAdapter<RewardListVO.DataBean, BaseViewHolder> {

    private Context mContext;

    public RewardListAdapter(List<RewardListVO.DataBean> mData, Context context) {
        super(R.layout.item_rewardlist, mData);
        this.mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, final RewardListVO.DataBean rewardVO) {
        TextView title = helper.getView(R.id.tv_title);
        title.setText(rewardVO.getTitle());

        LinearLayout layout = helper.getView(R.id.ll_rank_3);
        layout.removeAllViews();
        List<RewardListVO.DataBean.TicketUserBean> users = rewardVO.getTicketUser();
        int position = (users.size() < 3 ? users.size() : 3);
        for (int i=0; i<position; i++){
            View view = View.inflate(mContext, R.layout.item_reward_list_best, null);
            ImageView img_rank = view.findViewById(R.id.iv_reward_rank);
            ImageView img = view.findViewById(R.id.iv_reward_cover);
            TextView name = view.findViewById(R.id.tv_reward_name);
            if (i == 0){
                GlideUtil.display(mContext, img_rank, R.mipmap.first_reward_icon);
            }else if (i == 1){
                GlideUtil.display(mContext, img_rank, R.mipmap.second_reward_icon);
            }else{
                GlideUtil.display(mContext, img_rank, R.mipmap.three_reward_icon);
            }
            GlideUtil.displayCircle(mContext, img, rewardVO.getTicketUser().get(i).getAvatar());
            name.setText(rewardVO.getTicketUser().get(i).getNickname());
            layout.addView(view);
        }

        final LinearLayout ll_popular = helper.getView(R.id.ll_popular);
        RecyclerView recycler_rank = helper.getView(R.id.recycler_rank);
        if (users.size() > 3){
            List<RewardListVO.DataBean.TicketUserBean> popular_users = new ArrayList<>();
            for (int i = 3, length = users.size(); i < length; i++){
                popular_users.add(users.get(i));
            }
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            recycler_rank.setLayoutManager(linearLayoutManager);
            RewardListPopularAdater adater = new RewardListPopularAdater(popular_users);
            recycler_rank.setAdapter(adater);
        }

        final TextView tv_expand = helper.getView(R.id.tv_expand);
        tv_expand.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ll_popular.getVisibility() == View.VISIBLE){
                    //隐藏
                    tv_expand.setText("展开全部名单");
                    ll_popular.setVisibility(View.GONE);
                }else {

                    tv_expand.setText("收起全部名单");
                    ll_popular.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    public class RewardListPopularAdater extends BaseQuickAdapter<RewardListVO.DataBean.TicketUserBean, BaseViewHolder>{

        public RewardListPopularAdater( @Nullable List<RewardListVO.DataBean.TicketUserBean> data) {
            super(R.layout.item_reward_list_popular, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, RewardListVO.DataBean.TicketUserBean item) {
            ImageView img = helper.getView(R.id.iv_reward_cover);
            TextView name = helper.getView(R.id.tv_reward_name);
            GlideUtil.displayCircle(mContext, img, item.getAvatar());
            name.setText(item.getNickname());
        }
    }
}
